package com.cloudsigns.factorymanager2.project.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;

import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;

import com.cloudsigns.factorymanager2.event.EngineeringOrderDetailForTeamEvent;
import com.cloudsigns.factorymanager2.project.adapter.ProjectMainViewPageAdapter;
import com.cloudsigns.factorymanager2.project.bean.SurVeyOrderBean;
import com.cloudsigns.factorymanager2.project.customview.CustomViewPager;
import com.cloudsigns.factorymanager2.project.customview.PageIndicatorView;
import com.cloudsigns.factorymanager2.project.fragment.SurveyAddFeedBackForTeamFragment;
import com.cloudsigns.factorymanager2.project.fragment.SurveyWordOrderInfoForTeamFragment;
import com.cloudsigns.factorymanager2.project.iview.ISurveyOrderDetailForTeamView;
import com.cloudsigns.factorymanager2.project.presenter.SurveyOrderDetailForTeamPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;

/**
 * Created by tony on 2018/8/5.
 */

public class TeamSurVeyOrderFeedBackActivity  extends FragmentActivity implements View.OnClickListener,ISurveyOrderDetailForTeamView{
    private final String TAG = TeamSurVeyOrderFeedBackActivity.class.getSimpleName();
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_work_order_information, tv_daily_feed_back;
    private CustomViewPager viewpager;
    public static final int WORK_ORDER_INFORMATION = 0;
    public static final int DAILY_FEED_BACK = 1;

    private ArrayList<Fragment> listFragments;
    private ProjectMainViewPageAdapter adapter;
    private SurveyWordOrderInfoForTeamFragment workOrderInfomationForEngineeringInstallFragment;
    private SurveyAddFeedBackForTeamFragment dailyFeedBackForEngineeringInstallFragment;

    private View LL_work_order_information;
    private View ll_daily_feed_back;

    public int currentIndex = WORK_ORDER_INFORMATION;
    private SurveyOrderDetailForTeamPresenter surveyOrderDetailForTeamPresenter;
    private OrderParentBean orderParentBean;
    private SurVeyOrderBean surVeyOrderBeanr;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_team_survey_order_feedback);

        surveyOrderDetailForTeamPresenter = new SurveyOrderDetailForTeamPresenter(this);
        init();

        tv_title.setText(getResources().getString(R.string.engineering_patro));
        Intent mIntent = getIntent();
        if (null != mIntent) {
            orderParentBean = (OrderParentBean) mIntent.getSerializableExtra(ConstantUtil.KEY_1);
        }

//        EventBus.getDefault().register(this);// 注册

        getEngineeringOrderDetail();
    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        tv_work_order_information = findViewById(R.id.tv_work_order_information);
        tv_daily_feed_back = findViewById(R.id.tv_daily_feed_back);
        viewpager = (CustomViewPager) findViewById(R.id.viewpager);
        LL_work_order_information = findViewById(R.id.LL_work_order_information);
        ll_daily_feed_back = findViewById(R.id.ll_daily_feed_back);

        LL_work_order_information.setOnClickListener(this);
        ll_daily_feed_back.setOnClickListener(this);
        rl_back.setOnClickListener(this);


        workOrderInfomationForEngineeringInstallFragment = new SurveyWordOrderInfoForTeamFragment();
        dailyFeedBackForEngineeringInstallFragment = new SurveyAddFeedBackForTeamFragment();


        listFragments = new ArrayList<Fragment>();
        listFragments.add(workOrderInfomationForEngineeringInstallFragment);
        listFragments.add(dailyFeedBackForEngineeringInstallFragment);



        adapter = new ProjectMainViewPageAdapter(getSupportFragmentManager(), listFragments);
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(2);

        PageIndicatorView indicator = (PageIndicatorView) findViewById(R.id.indicator);
        indicator.setViewPager(viewpager);
        indicator.setFades(false);
        indicator.setSelectedColor(getResources().getColor(R.color.rgb_2E75B6));
        indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                showSelectedOption(position);

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    private void getEngineeringOrderDetail() {
        if (null != surveyOrderDetailForTeamPresenter)
            surveyOrderDetailForTeamPresenter.surveyOrderDetailForTeam();
    }


    /**
     * 切换到哪个tab页
     *
     * @param pos
     */
    private void showSelectedOption(int pos) {
        // TODO Auto-generated method stub
        switch (pos) {
            case WORK_ORDER_INFORMATION:
                LL_work_order_information.setSelected(true);
                ll_daily_feed_back.setSelected(false);

                tv_work_order_information.setTextColor(getResources().getColor(R.color.rgb_3333333));
                tv_daily_feed_back.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                break;
            case DAILY_FEED_BACK:
                LL_work_order_information.setSelected(false);
                ll_daily_feed_back.setSelected(true);

                tv_work_order_information.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_daily_feed_back.setTextColor(getResources().getColor(R.color.rgb_3333333));
                break;

            default:
                break;
        }
    }


//    public void onEventMainThread(EngineeringOrderDetailForTeamEvent event) {
//        getEngineeringOrderDetail();
//    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.LL_work_order_information:
                if (currentIndex != WORK_ORDER_INFORMATION) {

                    currentIndex = WORK_ORDER_INFORMATION;
                    viewpager.setCurrentItem(WORK_ORDER_INFORMATION);
                    showSelectedOption(WORK_ORDER_INFORMATION);
                }
                break;
            case R.id.ll_daily_feed_back:
                if (currentIndex != DAILY_FEED_BACK) {

                    currentIndex = DAILY_FEED_BACK;
                    viewpager.setCurrentItem(DAILY_FEED_BACK);
                    showSelectedOption(DAILY_FEED_BACK);
                }
                break;
        }
    }


    @Override
    public long getFeedbackDate() {
        return 0;
    }

    @Override
    public String getItemId() {
        return null;
    }

    @Override
    public String getOrderId() {
        if (null != orderParentBean)
            return orderParentBean.getId();
        return null;
    }

    @Override
    public void surveyOrderDetailForTeamSuccessed(SurVeyOrderBean surVeyOrderBean) {
        this.surVeyOrderBeanr = surVeyOrderBean;
        if (workOrderInfomationForEngineeringInstallFragment != null)
            workOrderInfomationForEngineeringInstallFragment.initData(surVeyOrderBean);

        if (dailyFeedBackForEngineeringInstallFragment != null)
            dailyFeedBackForEngineeringInstallFragment.initData(surVeyOrderBean);

    }

    @Override
    public void surveyOrderDetailForTeamFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }



    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//
//        EventBus.getDefault().unregister(this);//解注册
//        setResult(ConstantUtil.START_ACTIVITY_KEY_1, null);
    }
}
